package com.hongxun.design.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.UserBean;
import com.hongxun.design.listeners.MyCheckedChangeListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/5/20.
 */
public class MultiSelectAdapter extends BaseAdapter {
    List<UserBean> itemBean;
    Context context;
    LayoutInflater inflater;
    public Map<Integer, Boolean> map;
    private int type = 0;

    public MultiSelectAdapter(List<UserBean> itemBean, Context context, String id) {
        this.itemBean = itemBean;
        this.context = context;
        this.map = new HashMap<>();
        this.inflater = LayoutInflater.from(context);

        for (int i = 0; i < itemBean.size(); i++) {
            map.put(i, false);
        }
        if (!TextUtils.isEmpty(id)) {
            if (id.contains(",")) {
                String[] ids = id.split(",");
                for (int i = 0; i < itemBean.size(); i++) {
                    for (int j = 0; j < ids.length; j++) {
                        if (ids[j].equals(itemBean.get(i).getId())) map.put(i, true);
                    }
                }
            } else {
                for (int i = 0; i < itemBean.size(); i++) {
                    if (id.equals(itemBean.get(i).getId())) map.put(i, true);
                }
            }
        }
    }

    public void setPz(List<UserBean> itemBean) {
        map = new HashMap<>();
        for (int i = 0; i < itemBean.size(); i++) {
            map.put(i, false);
        }
        type = 1;
    }

    public Object getChecked() {
        List<UserBean> back = new ArrayList<>();
        for (int i = 0; i < itemBean.size(); i++) {
            if (map.get(i)) {
                back.add(itemBean.get(i));
            }
        }
        return back;
    }

    @Override
    public int getCount() {
        return itemBean.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        UserBean bean = itemBean.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_multiselect, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        if (type == 0) {
            holder.textView1.setVisibility(View.GONE);
            holder.textView.setText(" " + bean.getRealname());
            holder.multiselect_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CheckBox textView = (CheckBox) view.findViewById(R.id.multiselect_title);
                    if (textView.isChecked()) {
                        textView.setChecked(false);
                    } else {
                        textView.setChecked(true);
                    }
                }
            });
        } else if (type == 1) {
            holder.textView.setVisibility(View.GONE);
            holder.textView1.setVisibility(View.VISIBLE);
            holder.textView1.setText(" " + bean.getRealname());
        }

        holder.textView.setOnCheckedChangeListener(new MyCheckedChangeListener(i) {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked, int position) {
                map.put(position, isChecked);
            }
        });

        if (map.get(i)) {
            holder.textView.setChecked(true);
        } else {
            holder.textView.setChecked(false);
        }
//        holder.textView.setButtonDrawable(R.drawable.checkbox);

        return view;
    }

    class Holder {
        CheckBox textView;
        TextView textView1;
        RelativeLayout multiselect_item;

        Holder(View view) {
            multiselect_item = (RelativeLayout) view.findViewById(R.id.multiselect_item);
            textView = (CheckBox) view.findViewById(R.id.multiselect_title);
            textView1 = (TextView) view.findViewById(R.id.multiselect_title_tv);
        }
    }
}
